package net.worldscale.xp.wslib.wsxml;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public abstract class DomUtil {

	public static String getTextContent(Node node) {
		final StringBuffer sbuf = new StringBuffer();
		_getTextContent(sbuf, node);
		return sbuf.toString();
	}

	private static void _getTextContent(StringBuffer sbuf, Node node) {
		if (node instanceof Text) {
			Text text = (Text) node;
			sbuf.append(text.getData());
		} else if (node instanceof Element) {
			Element ele = (Element) node;
			NodeList nlist = ele.getChildNodes();
			int len = nlist.getLength();
			for (int i = 0; i < len; i++) {
				_getTextContent(sbuf, nlist.item(i));
			}
		}
	}
}
